package cn.wolfcode.p2p.mgrsite.web.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.query.BidRequestQuery;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 后台借款申请控制类
 */
@Controller
public class BidRequestController {
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRealAuthService realAuthService;

    @RequestMapping("/bidrequest_publishaudit_list")
    public String bidRequestPublishauditList(Model model, @ModelAttribute("qo") BidRequestQuery qo){
        // 查询待审核的借款申请
        qo.setBidRequestState(0);
        model.addAttribute("pageInfo", bidRequestService.query(qo));

        return "bidrequest/publish_audit";
    }

    /**
     * 审核
     */
    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public JSONResult bidrequestPublishAudit(Long id, String publishTime, Integer state, String remark){

        bidRequestService.PublishAudit(id, publishTime, state, remark);
        return new JSONResult();
    }

    /**
     * 查看借款详情
     */
    @RequestMapping("/borrow_info")
    public String borrowInfo(Model model, Long id){
        BidRequest br = bidRequestService.get(id);
        UserInfo userInfo = userInfoService.get(br.getCreateUser().getId());
        model.addAttribute("bidRequest", br);
        model.addAttribute("userInfo", userInfo);
        model.addAttribute("audits", bidRequestService.listAuditHistorysByBidRequestId(id));
        model.addAttribute("realAuth", realAuthService.get(userInfo.getRealAuthId()));


        return "bidrequest/borrow_info";
    }
}
